package smedi.ismedi.mobile.bff.infrastructure.util;

import com.github.dozermapper.core.DozerConverter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author Jimmy
 * @Date 2021/6/9 13:30
 */
public class DozerStringToLocalDateTimeConverter extends DozerConverter<String, LocalDateTime> {
    public DozerStringToLocalDateTimeConverter() {
        super(String.class, LocalDateTime.class);
    }

    @Override
    public LocalDateTime convertTo(String source, LocalDateTime localDateTime) {
        if (StringUtils.isBlank(source)) return null;
        LocalDateTime convertToDate =
                LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return convertToDate;
    }

    @Override
    public String convertFrom(LocalDateTime localDateTime, String s) {
        if (localDateTime == null) return null;
        String convertToString = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return convertToString;
    }
}